package com.auto.survey.activity.monitoring.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.auto.survey.activity.monitoring.entity.MonitorCheckPoint
import kotlinx.coroutines.flow.Flow

@Dao
interface MonitorCheckPointDao {

    // 基本操作
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(checkPoint: MonitorCheckPoint): Long

    @Update
    suspend fun update(checkPoint: MonitorCheckPoint)

    @Delete
    suspend fun delete(checkPoint: MonitorCheckPoint)

    @Query("SELECT * FROM MonitoringCheckPoint WHERE id = :id")
    suspend fun getById(id: Int): MonitorCheckPoint?

    @Query("SELECT * FROM MonitoringCheckPoint WHERE pointId = :pointId ORDER BY createTime DESC")
    suspend fun getByPointId(pointId: Int): List<MonitorCheckPoint>

    @Query("SELECT * FROM MonitoringCheckPoint WHERE isChecked = :checked")
    suspend fun getByCheckedStatus(checked: Boolean): List<MonitorCheckPoint>

    // 带 Flow 的实时查询
    @Query("SELECT * FROM MonitoringCheckPoint WHERE pointId = :pointId AND isPass = 1 ORDER BY createTime DESC")
    fun getPassedCheckPointsFlow(pointId: Int): Flow<List<MonitorCheckPoint>>
}